package com.yy.yjbo.yjbonetbroadcast;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * 有无网络的提醒
 * 通过EventBus通知,好处是可以全局响应；但是如果我们对是否有网只在一个界面中监听就不需要这么复杂了；
 * @author yjbo
 * @time 2017/2/10 11:17
 * @mail 1457521527@qq.com
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        System.out.println("===NetStateReceiver===当前网络状态==000==");

    }



    // 当监测到没网时，我们可能需要引导用户去打开网络：
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onMainEventBus(NetStatusClass msg) {
        isHaveNet();
    }
    private void isHaveNet() {
        boolean isNet = NetworkUtil.isConnected();
        if (!isNet) {
            NetworkUtil.getInstance().setNetworkMethod(this);
        }else {
            NetworkUtil.getInstance().setNetworkMethod(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
